package com.luo.chengrui.module.sa.controller.admin.commontree;

import com.luo.chengrui.module.sa.convert.org.OrgConvert;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;

import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import com.luo.chengrui.module.sa.controller.admin.commontree.vo.*;
import com.luo.chengrui.module.sa.dal.dataobject.commontree.CommonTreeDO;
import com.luo.chengrui.module.sa.convert.commontree.CommonTreeConvert;
import com.luo.chengrui.module.sa.service.commontree.CommonTreeService;

@Tag(name = "管理后台 - 通用树")
@RestController
@RequestMapping("/sa/common-tree")
@Validated
public class CommonTreeController {

    @Resource
    private CommonTreeService commonTreeService;

    @PostMapping("/create")
    @Operation(summary = "创建通用树")
    @PreAuthorize("@ss.hasPermission('sa:common-tree:create')")
    public CommonResult<String> createCommonTree(@Valid @RequestBody CommonTreeCreateReqVO createReqVO) {
        return success(commonTreeService.createCommonTree(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新通用树")
    @PreAuthorize("@ss.hasPermission('sa:common-tree:update')")
    public CommonResult<Boolean> updateCommonTree(@Valid @RequestBody CommonTreeUpdateReqVO updateReqVO) {
        commonTreeService.updateCommonTree(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除通用树")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('sa:common-tree:delete')")
    public CommonResult<Boolean> deleteCommonTree(@RequestParam("id") String id) {
        commonTreeService.deleteCommonTree(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得通用树")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('sa:common-tree:query')")
    public CommonResult<CommonTreeRespVO> getCommonTree(@RequestParam("id") String id) {
        CommonTreeDO commonTree = commonTreeService.getCommonTree(id);
        return success(CommonTreeConvert.INSTANCE.convert(commonTree));
    }

    @GetMapping("/list")
    @Operation(summary = "获得通用树列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('sa:common-tree:query')")
    public CommonResult<List<CommonTreeRespVO>> getCommonTreeList(@RequestParam("ids") Collection<String> ids) {
        List<CommonTreeDO> list = commonTreeService.getCommonTreeList(ids);
        return success(CommonTreeConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary = "获得通用树分页")
    @PreAuthorize("@ss.hasPermission('sa:common-tree:query')")
    public CommonResult<PageResult<CommonTreeRespVO>> getCommonTreePage(@Valid CommonTreePageReqVO pageVO) {
        PageResult<CommonTreeDO> pageResult = commonTreeService.getCommonTreePage(pageVO);
        return success(CommonTreeConvert.INSTANCE.convertPage(pageResult));
    }
    @GetMapping("/all")
    @Operation(summary = "获得通用树分页")
    @PreAuthorize("@ss.hasPermission('sa:common-tree:query')")
    public CommonResult<List<CommonTreeRespVO>> getCommonTreeAllList(@Valid CommonTreeExportReqVO pageVO) {
        List<CommonTreeDO> pageResult = commonTreeService.getCommonTreeAllList(pageVO);
        return success(CommonTreeConvert.INSTANCE.convertList(pageResult));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出通用树 Excel")
    @PreAuthorize("@ss.hasPermission('sa:common-tree:export')")
    @OperateLog(type = EXPORT)
    public void exportCommonTreeExcel(@Valid CommonTreeExportReqVO exportReqVO,
                                      HttpServletResponse response) throws IOException {
        List<CommonTreeDO> list = commonTreeService.getCommonTreeList(exportReqVO);
        // 导出 Excel
        List<CommonTreeExcelVO> datas = CommonTreeConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "通用树.xls", "数据", CommonTreeExcelVO.class, datas);
    }

    @GetMapping("/children")
    @Operation(summary = "查询子节点 ")
    @PreAuthorize("@ss.hasPermission('sa:common-tree:query')")
    @OperateLog(type = EXPORT)
    public CommonResult<List<CommonTreeRespVO>> queryCommonTreeListByParentId(@RequestParam("id") String id) throws IOException {
        List<CommonTreeDO> list = commonTreeService.getChildrenBy(id);
        return success(CommonTreeConvert.INSTANCE.convertList(list));
    }

}
